/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Acceso;

import Modelo.DAL.Personal_DAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author alb3rto
 */
public class cambiarPasswordAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        cambiarPasswordForm formBean = (cambiarPasswordForm) form;
        ActionErrors errors = new ActionErrors();
        boolean error = false;

        String user = formBean.getUsuario();
        String pwdActual = formBean.getPwdActual();
        String pwdNueva = formBean.getPwdNueva();
        String pwdNueva2 = formBean.getPwdNueva2();

        if (user == null || user.length() < 1) {
            errors.add("user", new ActionMessage("errors.user"));
            saveErrors(request, errors);
            error = true;
        }
        if (pwdActual == null || pwdActual.length() < 1) {
            errors.add("passwordRequerido", new ActionMessage("errors.passwordRequerido"));
            saveErrors(request, errors);
            error = true;
        }
        if (pwdNueva == null || pwdNueva.length() < 1) {
            errors.add("passwordRequerido", new ActionMessage("errors.passwordRequerido"));
            saveErrors(request, errors);
            error = true;
        } else if (pwdActual.equals(pwdNueva)) {
            errors.add("paswordIgual", new ActionMessage("errors.paswordIgual"));
            saveErrors(request, errors);
            error = true;
        }
        if (pwdNueva2 == null || pwdNueva2.length() < 1) {
            errors.add("passwordRequerido", new ActionMessage("errors.passwordRequerido"));
            saveErrors(request, errors);
            error = true;
        } else if (!pwdNueva.equals(pwdNueva2)) {
            errors.add("passwordsNoCoinciden", new ActionMessage("errors.passwordsNoCoinciden"));
            saveErrors(request, errors);
            error = true;
        }

        if (error) {
            System.out.println("ERROR");
            return mapping.findForward(FAILURE);
        }

        boolean success = Personal_DAO.cambiarPassword(user, pwdActual, pwdNueva);
        if (success) {
            System.out.print("Password cambiado correctamente");
            return mapping.findForward(SUCCESS);
        } else {
            return mapping.findForward(FAILURE);
        }
    }
}
